﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Finance.Entites;
using Finance.UI.ViewModels;

namespace Finance.UI
{
    public partial class SelectorPage : PhoneApplicationPage
    {
        private string returnLastUri = App.ReturnLastUri;
        private ConfigType set = ConfigType.Unknown;
        private ClassificationType classificationType = ClassificationType.Unknown;
        private string selected = string.Empty;

        public SelectorPage()
        {
            InitializeComponent();
            this.Loaded += (o, e) => { InitializeDataContext(); };
            this.Unloaded += (o, e) => { App.ReturnLastUri = string.Empty; };
        }

        private void InitializeDataContext()
        {
            if (string.IsNullOrEmpty(returnLastUri))
            { throw new ArgumentNullException("Return last uri can't found, please make sure the value returnLastUri in App isn't empty or null!"); }
            try
            {
                set = (ConfigType)Enum.Parse(typeof(ConfigType), NavigationContext.QueryString["set"], true);
                selected = NavigationContext.QueryString["selected"];
                classificationType = (ClassificationType)Enum.Parse(typeof(ClassificationType), NavigationContext.QueryString["classificationtype"], true);
            }
            catch { }
            if (set == ConfigType.Unknown) { NavigationService.GoBack(); }
            SelectorPageViewModel dataContext = null;
            switch (set)
            {
                case ConfigType.PayoutClassification:
                    {
                        dataContext = new SelectorPageViewModel(
                            "Payout Classification Selector",
                            selected,
                            DataContextBuilder.InitialClassifications<PayoutClassification>(
                            App.PayoutClassificationDataStore).ToArray());
                        break;
                    }
                case ConfigType.IncomeClassification:
                    {
                        dataContext = new SelectorPageViewModel(
                            "Income Classification Selector",
                            selected,
                            DataContextBuilder.InitialClassifications<IncomeClassification>(
                            App.IncomeClassificationDataStore).ToArray());
                        break;
                    }
                case ConfigType.Project:
                    {
                        dataContext = new SelectorPageViewModel(
                            "Project Selector",
                            selected,
                            DataContextBuilder.InitialProjects().ToArray());
                        break;
                    }
                case ConfigType.Account:
                    {
                        dataContext = new SelectorPageViewModel(
                            "Account Selector",
                            selected,
                            DataContextBuilder.InitialAccounts().ToArray());
                        break;
                    }
                case ConfigType.Trader:
                    {
                        dataContext = new SelectorPageViewModel(
                            "Trader Selector",
                            selected,
                            DataContextBuilder.InitialTraders().ToArray());
                        break;
                    }
                default: break;
            }
            this.DataContext = dataContext;
        }

        private void SelectItem_Click(object sender, EventArgs e)
        {
            var value = ((sender as RadioButton).DataContext as DetailItemViewModel).Name;
            NavigationService.Navigate(
                new Uri(
                    string.Format(
                    "{0}set={1}&value={2}&classificationtype={3}",
                    returnLastUri,
                    set,
                    value,
                    classificationType
                    ),
                    UriKind.RelativeOrAbsolute));
        }

        private void ApplicationBarIconBackButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}